package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zy on 2019/3/22.
 */
class Obj {
    String name;
    int age;
}

public class ThreadDeadLock {
    public static void main(String args[]) {
        //List<Obj> list = new ArrayList<>();
        Obj obj = new Obj();
        Obj obj1 = new Obj();
        obj.age = 10;
        obj.name = "name";
        //list.add(obj);
        obj.age = 15;
        //list.get(0).name = "list";
        System.out.println("equals:" + obj.equals(obj1));
        //System.out.println("==:" + (obj == list.get(0)));
        int a1 = 0;
        int b = 1;
        assert a1 == b; //需显示开启，默认为不开启状态
        assert a1 == b : "执行失败！";

        System.out.println("1234");
        //Scanner scanner = new Scanner(System.in);
        //try {
        //    int a = scanner.nextInt();
        //    System.out.print(a);
        //} catch (InputMismatchException e) {
        //    System.out.print("InputMismatchException");
        //}
        Info info = new Info();
        //info作为参数传入两个线程当中
        List list = new ArrayList();

        //线程同步问题测试
        ProducerThread pt = new ProducerThread(list);
        ConsumerThread ct = new ConsumerThread(list);
        Thread producer = new Thread(pt, "producer");
        Thread consumer = new Thread(ct, "consumer");
        producer.start();
        consumer.start();
    }

    //资源类
    static class Info {
        private String name = "name";
        private String content = "content";

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }

    // 生产者线程
    static class ProducerThread implements Runnable {
        private List list;
        private Info info = null;

        // 构造函数，其参数是资源
        public ProducerThread(Info info) {
            this.info = info;
        }

        public ProducerThread(List list) {
            this.list = list;
        }

        @Override
        public void run() {
            for (int i = 0; i < 1600; i++) {
//                if (i % 20 == 0) {
//                    System.out.println(i);
//                }
                list.add(i);
            }

            // boolean flag=false;
            //for (int i = 0; i < 10; i++) {
            //    this.info.setName("name" + i);
            //    try {
            //        Thread.sleep(100);
            //    } catch (InterruptedException e) {
            //        // TODO Auto-generated catch block
            //        e.printStackTrace();
            //    }
            //    this.info.setContent("content" + i);
            //}
        }
    }

    static class ConsumerThread implements Runnable {
        private List list;
        private Info info = null;

        // 构造函数，其参数是资源
        public ConsumerThread(Info info) {
            this.info = info;
        }

        public ConsumerThread(List list) {
            this.list = list;
        }

        @Override
        public void run() {
            for (int i = 0; i < 1600; i++) {
                list.get(i);
            }
            //for (int i = 0; i < 10; i++) {
            //    try {
            //        Thread.sleep(100);
            //    } catch (InterruptedException e) {
            //        // TODO Auto-generated catch block
            //        e.printStackTrace();
            //    }
            //    System.out.println(this.info.getName() + ":-->"
            //            + this.info.getContent());
            //}
        }
    }
}